-- NewApplyListRender

local NewTeamMemberHeadRender = import(".NewTeamMemberHeadRender")

local ClassRef = app.mvc.createRenderClass(...)

function ClassRef.create(node)
	local sprite = ClassRef.extend(cc.CSLoader:createNode(Res.NewApplyItemNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)
	-- print("NewApplyListRender.init()")

	self:setContentSize(self._ui["Panel"]:getContentSize())

	-- 通用操作
	self._ui["Panel/Button_1"]:addClickEventListener(function() -- 观察
		local metadata = self._data
		if not metadata then -- FriendModel:getOnlineFriends() or FriendModel:getRecents()
			metadata = self._data["info"]
		end
		executeFunc(self._onClick, "NewApplyListRender_Observe", metadata.uid )
	end)

	-- 申请相关
	self._ui["Panel/Button_2"]:addClickEventListener(function() -- 拒绝申请
		executeFunc(self._onClick, "NewApplyListRender_Refuse", { self._data.uid, false } )
	end)
	self._ui["Panel/Button_3"]:addClickEventListener(function() -- 同意申请
		executeFunc(self._onClick, "NewApplyListRender_Agree", { self._data.uid, true } )
	end)

	-- 邀请相关
	self._ui["Panel/Button_4"]:addClickEventListener(function() -- 私聊
		executeFunc(self._onClick, "NewApplyListRender_Whisper", self._data)
	end)
	self._ui["Panel/Button_5"]:addClickEventListener(function() -- 邀请
		executeFunc(self._onClick, "NewApplyListRender_InviteToTeam", self._data)
	end)

	-- 随从操作按钮
	self._partnerBtn = self._ui["Panel/Panel_partner/selected_btn"]
	self._partnerBtn:addClickEventListener(function() -- 切换随从 雇佣 状态
		if not self._partnerData then
			print( " ---------->>>> 没有随从数据，但是操作了随从雇佣状态按钮？" )
			return
		else
			self:setpartnerBtnStatus( not self:getpartnerBtnStatus() )
			executeFunc(self._onClick, "NewApplyListRender_ChangePartnerStatus", self)
		end
	end)

	self._head = NewTeamMemberHeadRender.create(self._ui["Panel/head"])
	-- self._head:onClick(function(sender)
	-- 	end)

	self._name = self._ui["Panel/name"]
	self._combatNum = self._ui["Panel/combat_num"]

	self._talentIcon = self._ui["Panel/Panel_bg/icon2"] -- 天赋图标
	self._talentName = self._ui["Panel/title"] -- 天赋名字

end

-- 以下5种数据结构中的一种
-- data = applyMembers -- 申请者
-- or FriendModel:getOnlineFriends()
-- or FriendModel:getRecents()
-- or NewguildModel:reqGuildMemberList()
-- or FieldManager:getNearbyUserNotInTeam()
function ClassRef:refresh()
	if self._data then
		local metadata = self._data
		if not metadata then -- FriendModel:getOnlineFriends() or FriendModel:getRecents()
			metadata = self._data["info"]
		end
		-- if not metadata then -- FieldManager:getNearbyUserNotInTeam() or NewguildModel:reqGuildMemberList()
		-- 	metadata = self._data
		-- end
		assert( metadata, " --------------------->>>>>> NewApplyListRender 没有数据？" )

		self._name:setString( metadata.name )
		self._combatNum:setString( metadata.combatPower )

		metadata.classID = metadata["classID"] or metadata["classId"]
		metadata.lv = metadata["lv"] or metadata["level"]
		self._head:setData( metadata )

		local skillChooseIdList = string.split( metadata.specialistID, "0" )
		self._talentName:setString( L("class_" .. skillChooseIdList[1] .. "_" .. skillChooseIdList[2] ) .. L("class_"..metadata.classID) )

		local calssIconPath = GD:getClassIcon( metadata.classID, 2 )
		self._talentIcon:loadTexture( calssIconPath )
	end
end

function ClassRef:setPartnerData( partnerInfoTemp )
	self._partnerData = partnerInfoTemp
	self:setName( self._partnerData:getName() )
	self:setCombatPower( self._partnerData:getCombatPower() )
	self:setHeadIcon( self._partnerData:getIcon() )
	self:setClassIcon( self._partnerData:getJobIcon() )
	self:setLevelStr( self._partnerData:getLevel() )
	self:setQualityIcon( self._partnerData:getQuality() )

	self:setTalentStr( self._partnerData:getJobName() )
	self:setTalentIcon(self._partnerData:getJobIcon())

	self:setpartnerBtnStatus( not self._partnerData:isEquiped() )
end

function ClassRef:setName( nameTemp )
	nameTemp = nameTemp or ""
	self._name:setString( nameTemp )
end

function ClassRef:setCombatPower( combatPowerTemp )
	combatPowerTemp = combatPowerTemp or ""
	self._combatNum:setString( combatPowerTemp )
end

function ClassRef:setHeadIcon( iconPath )
	if iconPath then
		self._head:setVisible( true )
		self._head:setIcon( iconPath )
	end
end
function ClassRef:setQualityIcon( quality )
	quality = quality or 1
	self._head:setVisible( true )
	self._head:setQualityIcon( quality )
end
function ClassRef:setClassIcon( iconPath )
	if iconPath then
		self._head:setVisible( true )
		self._head:setClassIcon( iconPath )
	end
end
function ClassRef:setLevelStr( lvTemp )
	lvTemp = lvTemp or ""
	self._head:setVisible( true )
	self._head:setLv( lvTemp )
end

function ClassRef:setTalentStr( talentNameTemp )
	talentNameTemp = talentNameTemp or ""
	self._talentName:setString( talentNameTemp )
end

function ClassRef:setTalentIcon( iconPath )
	if iconPath then
		self._talentIcon:loadTexture( iconPath )
	end
end

function ClassRef:getpartnerBtnStatus()
	return self._partnerBtn:isBright()
end
function ClassRef:setpartnerBtnStatus( bright )
	self._partnerBtn:setBright( bright )
end

-- 根据显示类型决定按钮的显示状态
-- 1-观察、同意、拒绝 可见
-- 2-观察、密聊、邀请 可见
-- 3-只有 Panel_partner 可见
function ClassRef:setShowMode( showMode )
	for i = 2, 5 do
		self._ui["Panel/Button_"..i]:setVisible( false )
	end

	if 1 == showMode then
		self._ui["Panel/Button_1"]:setVisible( true )
		self._ui["Panel/Button_2"]:setVisible( true )
		self._ui["Panel/Button_3"]:setVisible( true )
		self._ui["Panel/Button_4"]:setVisible( false )
		self._ui["Panel/Button_5"]:setVisible( false )
		self._ui["Panel/Panel_partner"]:setVisible( false )
	elseif 2 == showMode then
		self._ui["Panel/Button_1"]:setVisible( true )
		self._ui["Panel/Button_2"]:setVisible( false )
		self._ui["Panel/Button_3"]:setVisible( false )
		self._ui["Panel/Button_4"]:setVisible( true )
		self._ui["Panel/Button_5"]:setVisible( true )
		self._ui["Panel/Panel_partner"]:setVisible( false )
	elseif 3 == showMode then
		self._ui["Panel/Button_1"]:setVisible( false )
		self._ui["Panel/Button_2"]:setVisible( false )
		self._ui["Panel/Button_3"]:setVisible( false )
		self._ui["Panel/Button_4"]:setVisible( false )
		self._ui["Panel/Button_5"]:setVisible( false )
		self._ui["Panel/Panel_partner"]:setVisible( true )
	end
end

function ClassRef:onBtnClick(func)
	self._onClick = func
end

return ClassRef
